<?php

/**
 * 
 */
class Default_Plugin_Acl extends Zend_Controller_Plugin_Abstract {

    private $_controller = array(
        'module' => 'default',
        'controller' => 'error',
        'action' => 'denied'
    );

    public function __construct() {
        $acl = new Zend_Acl();

        //	ROLES
        $acl->addRole(new Zend_Acl_Role('guest'));
        $acl->addRole(new Zend_Acl_Role('male'));
		$acl->addRole(new Zend_Acl_Role('female'));
		$acl->addRole(new Zend_Acl_Role('agency'));
        $acl->addRole(new Zend_Acl_Role('admin'));

        //	RESOURCES
		//	DEFAULT MODULE RESOURCES
        $acl->addResource(new Zend_Acl_Resource('default'));
        $acl->addResource(new Zend_Acl_Resource('default:index'), 'default');
        $acl->addResource(new Zend_Acl_Resource('default:portfolio'), 'default');
        $acl->addResource(new Zend_Acl_Resource('default:about'), 'default');
        $acl->addResource(new Zend_Acl_Resource('default:contact'), 'default');
        $acl->addResource(new Zend_Acl_Resource('default:services'), 'default');
        $acl->addResource(new Zend_Acl_Resource('default:news'), 'default');
        // CONTROL MODULE RESOURCE //
        $acl->addResource(new Zend_Acl_Resource('control'));
        $acl->addResource(new Zend_Acl_Resource('control:index'), 'control');
        $acl->addResource(new Zend_Acl_Resource('control:portfolio'), 'control');
        $acl->addResource(new Zend_Acl_Resource('control:services'), 'control');
        $acl->addResource(new Zend_Acl_Resource('control:about'), 'control');
        $acl->addResource(new Zend_Acl_Resource('control:news'), 'control');
        $acl->addResource(new Zend_Acl_Resource('control:contact'), 'control');
        $acl->addResource(new Zend_Acl_Resource('control:sub-page'), 'control');
        $acl->addResource(new Zend_Acl_Resource('control:scroller'), 'control');
        $acl->addResource(new Zend_Acl_Resource('control:testimonials'), 'control');
        $acl->addResource(new Zend_Acl_Resource('control:settings'), 'control');
        $acl->addResource(new Zend_Acl_Resource('control:messages'), 'control');

        /* GUEST permissions */
        $acl->allow('guest', 'default');
        $acl->allow('guest', 'control');


        /* MALE permissions */

		/* FEMALE permissions */

		/* AGENCY permissions */
        
        /* ADMIN permissions */
        $acl->allow('admin', 'default');


        Zend_Registry::set('acl', $acl);
    }

    public function preDispatch(Zend_Controller_Request_Abstract $request) {
        $auth = Zend_Auth::getInstance();
        $acl = Zend_Registry::get('acl');

        if ($auth->hasIdentity()) {
            $role = $auth->getIdentity()->type;
        } else {
            $role = 'guest';
        }

        if (!$acl->hasRole($role)) {
            $role = 'guest';
        }
        $controller = $request->module . ':' . $request->controller;
        $action = $request->action;

        if (!$acl->has($controller)) {
            $controller = null;
        }
//    if(!$acl->has($module)) {
//	$module = null;
//    }

        if (!$acl->isAllowed($role, $controller, $action)) {
            $request->setModuleName($this->_controller['module']);
            $request->setControllerName($this->_controller['controller']);
            $request->setActionName($this->_controller['action']);
        }
    }

}